import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import { BasicMarker } from '../../js/BasicMarker.js';
import { BasicMarkers } from '../../js/BasicMarkers.js';
import { getMarkerIcon } from '../../js/icon.js';
import { handleCommonRes } from '@gis/lib/utils/resolver.js';
import { getDwCoordinateData, getClCoordinateData } from '@gis/lib/api';
import { netAndScenicDataStore } from '@gis/lib/pinia/netAndScenic.js';
import { openGridTeamPeopleDetails } from '../../../../views/grid-team/opt.js';

const typeCodeEnum = {
  zz_01_01: 'wanggey', // 综合执法 - 人员
  zz_01_07: 'zongzhi', // 网格员
  zz_02_03: 'zhzfc', // 综合执法车
};

class Marker extends BasicMarker {
  constructor({ map, data }) {
    super({ map, data });
    /* this.type: 这个type主要用于筛选,需要和filter-legend里面的type相对应 */
    this.type = typeCodeEnum[data.type_code]; //很多种类型，没有的使用’其他‘
    super.create();
  }
  getIcon() {
    return getMarkerIcon(this.type);
  }
  getIconOption(icon) {
    return {
      content: `<div style="width: 52px;height: 96px;background: url(${icon}) center center no-repeat;background-size: 100% 100%"></div>`,
      offset: new AMap.Pixel(0, 0),
    };
  }
  onClick() {
    if (this.data.type_code == 'zz_01_01' || this.data.type_code == 'zz_01_07') {
      openGridTeamPeopleDetails(this.data);
    }
  }
}

export default class Cyxldw extends BasicMarkers {
  constructor({ map }) {
    super();
    this.map = map;
  }
  createMarker(data) {
    return new Marker({ map: this.map, data });
  }
  async getDataAsync() {
    const { area_code } = useUserInfoStore();
    const { scenicIds } = netAndScenicDataStore();
    const params = {
      dType: 'dw',
      area_code: scenicIds && scenicIds.split(',').length == 1 ? scenicIds : area_code,
    };
    // 队伍在线人员 - 综合执法/网格员
    let data = await handleCommonRes(getDwCoordinateData, params);
    data = data.filter((item) => item.type_code == 'zz_01_01' || item.type_code == 'zz_01_07');

    const clParams = {
      dType: 'cl',
      area_code: scenicIds ? scenicIds : area_code,
    };
    let clData = await handleCommonRes(getClCoordinateData, clParams);
    // 综合执法车
    clData = clData.filter((item) => item.type_code == 'zz_02_03');

    return data.concat(clData).map((item) => Object.assign(item, { lnglat: [item.longitude, item.latitude] }));
  }
}
